# report.py
import sqlite3
import pandas as pd

def save_to_db(tx, ai_result):
    conn = sqlite3.connect('dbguardian.db')
    cursor = conn.cursor()
    cursor.execute('''
        INSERT INTO tx_risk_log 
        (timestamp, duration, rows_affected, risk_score, is_risk, suggestion)
        VALUES (?, ?, ?, ?, ?, ?)
    ''', (
        pd.Timestamp.now(),
        tx['duration_sec'],
        tx['rows_affected'],
        ai_result['risk_score'],
        ai_result['is_long_running'],
        ai_result['suggestion']
    ))
    conn.commit()
    conn.close()